package com.headwind.logistics.order.controller;


import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.pojo.GoodsIssueType;
import com.headwind.logistics.common.util.LayuiUtil;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.order.dto.OrderDto;
import com.headwind.logistics.order.entity.Order;
import com.headwind.logistics.order.entity.OrderCargo;
import com.headwind.logistics.order.entity.OrderServiceFee;
import com.headwind.logistics.order.entity.OrderTail;
import com.headwind.logistics.order.service.inter.OrderServiceInter;
import org.springframework.web.bind.annotation.*;
import org.json.JSONException;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 盛夏
 * @Date 2020-07-20 上午 9:54
 * @Version 1.0
 * @DESCRIPTION 填写类作用描述
 */
@RestController
public class OrderController {

    @Resource
    private OrderServiceInter orderInter;

    @Resource
    private ResultGenerator resultGenerator;

    /**
     * 新增订单
     * @param beginBranch   开始网点主键
     * @param endBranch     结束网点主键
     * @param cargoJson     订单货物数据
     * @param costJson      订单费用数据
     * @param courierOdd    面单号
     * @param planOrder     预录单数据
     * @param session       session作用域
     * @return              状态码
     * @throws JSONException    异常
     */
    @PostMapping("/admin/order/addOrder")
    public int addOrder(int beginBranch, int endBranch, String cargoJson, String costJson, String courierOdd,
                        String planOrder, HttpSession session) throws JSONException{




        return orderInter.addOrder(beginBranch,endBranch,courierOdd,session,planOrder,cargoJson,costJson);
    }

    /**
     * 查询所有异常类型
     * @return  结果集
     */
    @GetMapping("/admin/order/getGoodsIssueType")
    public RestResult getGoodsIssueType(){

        List<GoodsIssueType> list = orderInter.queryGoodsIssueType();

        return resultGenerator.getSuccessResult(list);
    }

    /**
     * 根据面单号段查订单
     * @return
     */
    @GetMapping("/admin/order/findOrderByCourierOdd")
    public RestResult findOrderByCourierOdd(String expressGrantStartNum,String expressGrantEndNum){

        return resultGenerator.getSuccessResult(orderInter.queryOrderByExpressGrantOdd(expressGrantStartNum,expressGrantEndNum));
    }

    @RequestMapping("/admin/order/queryOrderPage")
    public LayuiUtil queryOrderPage(Integer page, Integer limit,HttpSession session,String orderStatus){
        Employee employee = (Employee)session.getAttribute("EMPLOYEE");
        System.out.println("查新的订单状态"+orderStatus);
        return LayuiUtil.data(orderInter.queryOrderCount(),orderInter.queryOrderByPage(page,limit,employee.getEmployeeId()));
    }

    /**
     *根据订单号查询订单费用
     * @param orderOdd  订单号
     * @return
     */
    @GetMapping("/admin/order/getOrderServiceFee/{orderOdd}")
    public LayuiUtil getOrderServiceFee(@PathVariable("orderOdd") String orderOdd){

        List<OrderDto> list = orderInter.queryServiceFeeByOrderOdd(orderOdd);
        System.out.println(list.get(0).getOrderOdd()+"\t"+list.get(0).getDiscount());
        return LayuiUtil.data(list.size(),list);
    }


    /**
     * 查询追踪信息
     * @param orderOdd
     * @return
     */
    @GetMapping("/admin/order/getOrderTail/{orderOdd}")
    public LayuiUtil getOrderTail(@PathVariable("orderOdd") String orderOdd){

        List<OrderTail> orderTails = orderInter.queryOrderTailByOrderId(orderOdd);

        return LayuiUtil.data(orderTails.size(),orderTails);
    }

    /**
     * 逻辑删除
     * @param orderOdd
     * @param session
     * @return
     */
    @PostMapping("/admin/order/logicDeleteOrder")
    public RestResult logicDeleteOrder(String orderOdd,HttpSession session){
        return resultGenerator.getSuccessResult(orderInter.loginDeleteOrder(orderOdd,session));
    }
}
